package com.farm.listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.farm.pojo.Carousel;
import com.farm.pojo.Policy;
import com.farm.pojo.User;
import com.farm.pojo.Video;
import com.farm.service.CarouselService;
import com.farm.service.PolicyService;
import com.farm.service.UserService;
import com.farm.service.VideoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
//@Component
@Slf4j
public class EveryAudit {
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    @Autowired
    private UserService userService;

    @Autowired
    private VideoService videoService;

    @Autowired
    private PolicyService policyService;

    @Autowired
    private CarouselService carouselService;

    //用户审核
    @KafkaListener(topics = "farm.user")
    public void userChangeState(String message){
        if(!StringUtils.isEmpty(message)){
            String storedUserJson = (String) redisTemplate.opsForValue().get(message);
            User user = JSON.parseObject(storedUserJson, new TypeReference<User>() {});
            userService.updateById(user);
            redisTemplate.delete(message);
        }
    }

    //视频审核
    @KafkaListener(topics = "farm.video")
    public void videoChangeState(String message){
        if(!StringUtils.isEmpty(message)){
            String storedUserJson = (String) redisTemplate.opsForValue().get(message);
            Video video = JSON.parseObject(storedUserJson, new TypeReference<Video>() {});
            videoService.updateById(video);
            redisTemplate.delete(message);
        }
    }

    //视频审核
    @KafkaListener(topics = "farm.policy")
    public void policyChangeState(String message){
        if(!StringUtils.isEmpty(message)){
            String storedUserJson = (String) redisTemplate.opsForValue().get(message);
            Policy policy = JSON.parseObject(storedUserJson, new TypeReference<Policy>() {});
            policyService.updateById(policy);
            redisTemplate.delete(message);
        }
    }

    //轮播图审核
    @KafkaListener(topics = "farm.carousel")
    public void carouselChangeState(String message){
        if(!StringUtils.isEmpty(message)){
            String storedUserJson = (String) redisTemplate.opsForValue().get(message);
            Carousel carousel = JSON.parseObject(storedUserJson, new TypeReference<Carousel>() {});
            carouselService.updateById(carousel);
            redisTemplate.delete(message);
        }
    }
}
